home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 11 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  10.6 KB  |  392 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.  
  18.     kIconWindowId =        1000;                        { Id of the icon window 'view' resource.}
  19.     kIconViewId =        1001;                        { Id of the TIconView resource.            }
  20.  
  21.     
  22.     { Constants for TIconView }
  23.  
  24.     kDefaultMagnification =    7;                        { Default icon magnification.            }
  25.     kBorder =                5;                        { Border in which to inset drawing.        }
  26.  
  27.  
  28.  
  29.  
  30. TYPE
  31.     LongArrayHdl =        ^LongArrayPtr;
  32.     LongArrayPtr =        ^LongArray;
  33.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  34.  
  35.  
  36.  
  37. {-------------------------------------------------------------------------------------------}
  38. {--------------------------------TIconApplication methods-----------------------------------}
  39. {-------------------------------------------------------------------------------------------}
  40.  
  41. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  42.  
  43. VAR anIconView : TIconView;
  44.  
  45. BEGIN
  46.     IApplication(iconFileType);
  47.     
  48.     if gCreateWithTemplates then begin  { Make sure the linker doesn't strip out view code. }
  49.         New(anIconView);
  50.     end;
  51. END;
  52.  
  53.  
  54.  
  55. {-------------------------------------------------------------------------------------------}
  56.  
  57. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  58.  
  59. VAR
  60.     anIconDocument:        TIconDocument;
  61.  
  62. BEGIN
  63.     New(anIconDocument);                            { Create a TIconDocument object.        }
  64.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  65.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  66.  
  67.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  68. END;
  69.     
  70.     
  71.     
  72.     
  73. {-------------------------------------------------------------------------------------------}
  74. {----------------------------------TIconDocument methods------------------------------------}
  75. {-------------------------------------------------------------------------------------------}
  76.  
  77.  
  78. PROCEDURE TIconDocument.IIconDocument;
  79.  
  80. VAR anIconBitMap : TIconBitMap;
  81.  
  82. BEGIN
  83.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  84.                                                     { fails, TIconDocument.Free works okay.    }
  85.  
  86.     IDocument(kFileType,                             { This document's file type.            }
  87.               kSignature,                             { This document's creator.                }
  88.               kUsesDataFork,                         { This document does use the data fork    }
  89.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  90.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  91.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  92.  
  93.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  94.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  95.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  96.  
  97.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.    }
  98. END;
  99.  
  100.  
  101.  
  102. {-------------------------------------------------------------------------------------------}
  103.  
  104. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  105.     { This method is called to set the document's data to the "new" state, as when the user    }
  106.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  107.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  108.     { can the document's initial state simply by changing the "seed" icon resource.            }
  109.  
  110. VAR
  111.     seedIcon:        Handle;
  112.  
  113. BEGIN
  114.     seedIcon := GetIcon(kSeedIconId);                { Get the seed icon resource.            }
  115.     FailNilResource(seedIcon);
  116.     fIconBitMap.SetIconBitMap(seedIcon)
  117. END;
  118.  
  119.  
  120.  
  121. {-------------------------------------------------------------------------------------------}
  122.  
  123. PROCEDURE TIconDocument.Free; OVERRIDE;
  124.     
  125. BEGIN
  126.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  127.  
  128.     INHERITED Free;
  129. END;
  130.  
  131.  
  132.  
  133. {-------------------------------------------------------------------------------------------}
  134.  
  135. PROCEDURE TIconDocument.DoMakeViews (forPrinting: BOOLEAN); OVERRIDE;
  136.  
  137. VAR
  138.     aWindow:  TWindow;
  139.  
  140. BEGIN
  141.     aWindow := NewTemplateWindow(kIconWindowId, SELF);        { Create the view hierarchy }
  142. END;
  143.  
  144.  
  145.     
  146. {-------------------------------------------------------------------------------------------}
  147.  
  148. PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
  149.                                                    fieldAddr: Ptr;
  150.                                                    fieldType: INTEGER)); OVERRIDE;
  151.  
  152. BEGIN
  153.     DoToField('TIconDocument', NIL, bClass);
  154.     DoToField('fIconBitMap', @fIconBitMap, bObject);
  155.  
  156.     INHERITED Fields(DoToField);
  157. END;
  158.  
  159.  
  160. {-------------------------------------------------------------------------------------------}
  161. {------------------------------------TIconView methods--------------------------------------}
  162. {-------------------------------------------------------------------------------------------}
  163.  
  164.  
  165. PROCEDURE TIconView.IRes (itsDocument: TDocument; itsSuperView: TView; VAR itsParams: Ptr);
  166.  
  167. BEGIN
  168.     INHERITED IRes(itsDocument, itsSuperView, itsParams);
  169.  
  170.     fMagnification := kDefaultMagnification;
  171.     fIconDocument := TIconDocument(itsDocument);
  172. END;
  173.  
  174.  
  175.  
  176. {-------------------------------------------------------------------------------------------}
  177.  
  178. PROCEDURE TIconView.CalcMinSize (VAR minSize: VPoint); OVERRIDE;
  179.  
  180. BEGIN
  181.     minSize.h := kIconHBits * fMagnification + kBorder + kBorder;
  182.     minSize.v := kIconVBits * fMagnification + kBorder + kBorder;
  183. END;
  184.  
  185.  
  186.  
  187.  
  188. {-------------------------------------------------------------------------------------------}
  189.  
  190. PROCEDURE TIconView.Draw (area: Rect); OVERRIDE;
  191.  
  192. VAR
  193.     drawingRect:    Rect;
  194.  
  195. BEGIN
  196.     SetRect(drawingRect, kBorder, kBorder,
  197.                          kBorder + (kIconHBits * fMagnification),
  198.                          kBorder + (kIconVBits * fMagnification));
  199.  
  200.     fIconDocument.fIconBitMap.Draw(drawingRect);    
  201. END;
  202.  
  203.  
  204. {-------------------------------------------------------------------------------------------}
  205.  
  206. PROCEDURE TIconView.Fields (PROCEDURE DoToField (fieldName: Str255;
  207.                                                  fieldAddr: Ptr;
  208.                                                  fieldType: INTEGER)); OVERRIDE;
  209.                                                  
  210. BEGIN
  211.     DoToField('TIconView', NIL, bClass);
  212.     DoToField('fIconDocument', @fIconDocument, bObject);
  213.     DoToField('fMagnification', @fMagnification, bInteger);
  214.     
  215.     INHERITED Fields(DoToFIeld);
  216. END;
  217.  
  218.  
  219.  
  220. {-------------------------------------------------------------------------------------------}
  221. {-----------------------------------TIconBitMap methods-------------------------------------}
  222. {-------------------------------------------------------------------------------------------}
  223.  
  224. PROCEDURE TIconBitMap.IIconBitMap;
  225.  
  226. BEGIN
  227.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  228.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  229. END;
  230.  
  231.  
  232.  
  233. {-------------------------------------------------------------------------------------------}
  234.  
  235. PROCEDURE TIconBitMap.Free; OVERRIDE;
  236.  
  237. BEGIN
  238.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  239. END;
  240.  
  241.  
  242.  
  243. {-------------------------------------------------------------------------------------------}
  244.  
  245. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  246.  
  247. BEGIN
  248.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  249.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  250. END;
  251.             
  252.             
  253.  
  254. {-------------------------------------------------------------------------------------------}
  255.  
  256. PROCEDURE TIconBitMap.Clear;
  257.  
  258. VAR
  259.     iconAsLongArray:    LongArrayHdl;
  260.     i:                    INTEGER;
  261.  
  262. BEGIN
  263.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  264.  
  265.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  266.         iconAsLongArray^^[i] := 0;
  267. END;
  268.  
  269.  
  270.  
  271. {-------------------------------------------------------------------------------------------}
  272.  
  273. PROCEDURE TIconBitMap.Invert;
  274.  
  275. VAR
  276.     iconAsLongArray:    LongArrayHdl;
  277.     i:                    INTEGER;
  278.  
  279. BEGIN
  280.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  281.  
  282.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  283.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  284. END;
  285.  
  286.  
  287.  
  288. {-------------------------------------------------------------------------------------------}
  289.  
  290. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  291.     { This converts the given icon bit to a word and bit in an array of long words. }
  292.  
  293. VAR
  294.     bitNumber:        INTEGER;
  295.  
  296. BEGIN
  297.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  298.     word := bitNumber DIV 32;
  299.     bit := 31 - (bitNumber MOD 32);
  300. END;
  301.  
  302.  
  303.  
  304. {-------------------------------------------------------------------------------------------}
  305.  
  306. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  307.     { Returns the state of the given bit in the icon being drawn. }
  308.  
  309. VAR
  310.     word:            INTEGER;
  311.     bitInWord:        INTEGER;
  312.  
  313. BEGIN
  314.     IconBitToWordBit(iconBit, word, bitInWord);
  315.  
  316.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  317. END;
  318.  
  319.  
  320.  
  321. {-------------------------------------------------------------------------------------------}
  322.  
  323. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  324.     { Set the state of the given bit in the icon being drawn. }
  325.  
  326. VAR
  327.     word:            INTEGER;
  328.     bitInWord:        INTEGER;
  329.  
  330. BEGIN
  331.     IconBitToWordBit(iconBit, word, bitInWord);
  332.  
  333.     {$H-}                                            { So the compiler thinks this is unsafe.}
  334.     IF turnBitOn THEN
  335.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  336.     ELSE
  337.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  338.     {$H+}
  339. END;
  340.  
  341.  
  342.  
  343. {-------------------------------------------------------------------------------------------}
  344.  
  345. FUNCTION TIconBitMap.Copy: TIconBitMap;
  346.  
  347. VAR
  348.     copyOfIcon:        TIconBitMap;
  349.  
  350. BEGIN
  351.     New(copyOfIcon);                                { Create a TIcon object.                }
  352.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  353.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  354.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  355.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  356. END;
  357.  
  358.  
  359.             
  360. {-------------------------------------------------------------------------------------------}
  361.  
  362. PROCEDURE TIconBitMap.Draw (area: Rect);
  363.  
  364. BEGIN
  365.     PlotIcon(area, fDataHandle);    
  366. END;
  367.  
  368.  
  369.  
  370. {-------------------------------------------------------------------------------------------}
  371.  
  372. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  373.  
  374. BEGIN
  375.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  376. END;
  377.  
  378.  
  379. {-------------------------------------------------------------------------------------------}
  380.  
  381. PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
  382.                                                    fieldAddr: Ptr;
  383.                                                    fieldType: INTEGER)); OVERRIDE;
  384.  
  385. BEGIN
  386.     DoToField('TIconBitMap', NIL, bClass);
  387.     DoToField('fDataHandle', @fDataHandle, bHandle);
  388.  
  389.     INHERITED Fields(DoToField);
  390. END;
  391.  
  392.